package com.view.cn.mvprxjava.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.view.cn.mvprxjava.R;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Retrolambda表达式
 * Created by Administrator on 2016/9/22 0013.
 */
public class RetrolambdaFragment extends BaseFragment {

	public static final String TAG = RetrolambdaFragment.class.getSimpleName();
	@Bind(R.id.tv_lambda1)
	TextView tvLambda1;
	@Bind(R.id.tv_lambda2)
	TextView tvLambda2;
	@Bind(R.id.tv_lambda3)
	TextView tvLambda3;

	public static RetrolambdaFragment newInstance() {
		RetrolambdaFragment fragment = new RetrolambdaFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_lambda, container, false);

		initView(view);

		initEvent();

		return view;
	}

	private void initEvent() {
		lambda1();
		lambda2();
		lambda3();
	}

	//无输入 void === () -> body
	private void lambda1() {
		//可以把下面注释的代码写成lambda的形式
//		tvLambda1.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				tvLambda1.setText("点击了tv1");
//			}
//		});
		tvLambda1.setOnClickListener(v -> {
			//可以把下面注释的代码写成lambda的形式
			tvLambda1.setText("点击了tv1");
		});

	}

	//一个参数输入 === x -> body
	private void lambda2() {
		//可以把下面注释的代码写成lambda的形式
//		tvLambda2.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				tvLambda2.setText("点击了tv2");
//			}
//		});
		tvLambda2.setOnClickListener(v -> tvLambda2.setText("点击了tv2"));
	}

	//多个参数输入 === (x, y) -> x + y;
	private void lambda3() {
		tvLambda3.setOnClickListener(v -> {
			List<String> names = Arrays.asList("aaa", "张三", "李四", "王二", "麻子");
			//可以把下面注释的代码写成lambda的形式
			Collections.sort(names, new Comparator<String>() {
				@Override
				public int compare(String x, String y) {
					return y.compareTo(x);
				}
			});
			Collections.sort(names, (x, y) -> y.compareTo(x));
			tvLambda3.setText(names.toString());
		});
	}


	private void initView(View view) {
		ButterKnife.bind(this, view);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}
}
